using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using System;
using Unity.VisualScripting;

partial struct CreateEnemySystem : ISystem
{
    Unity.Mathematics.Random rand;
    float timer;
    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<GamePreComponent>();
        rand=new Unity.Mathematics.Random((uint)6984);
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        if(timer<2f)timer+=SystemAPI.Time.DeltaTime;
        else{
        var gamePre=SystemAPI.GetSingleton<GamePreComponent>();
        var player=SystemAPI.GetSingletonEntity<PlayerControlComponent>();
        var playerTrans=SystemAPI.GetComponentRO<LocalTransform>(player);
        Entity enemy= state.EntityManager.Instantiate(gamePre.enemyPre);
        var tran=SystemAPI.GetComponentRW<LocalTransform>(enemy);
        tran.ValueRW.Position=new float3(rand.NextFloat(-6,6),rand.NextFloat(-6,6),0) +playerTrans.ValueRO.Position;
        timer=0;
        }
    }

    [BurstCompile]
    public void OnDestroy(ref SystemState state)
    {
        
    }
}
